/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.RolDao;
import edu.upc.clase.demo.entity.Rol;
import edu.upc.clase.demo.entity.Usuario;
import java.util.List;

import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;



/**
 *
 * @author Jose
 */
@Repository
public class RolDaoImpl  extends SimpleJdbcDaoSupport implements RolDao{
    
    private static Logger log = LoggerFactory.getLogger(RolDaoImpl.class);

    @Autowired
    public RolDaoImpl(DataSource dataSource) {
        log.info("Asignando el dataSource");
        setDataSource(dataSource);
    }
    @Override
    public Integer insertar(Rol rol)
     {
       getJdbcTemplate().update("insert into Rol (Descripcion) values (?)",
                rol.getdescripcion());
       return getSimpleJdbcTemplate().queryForInt("select last_insert_id()");
     }
    @Override
    public void actualizar(Rol rol)
     {
        getJdbcTemplate().update("update Rol set descripcion =? where Id_rol = ?",
              rol.getdescripcion()  , rol.getid_rol());    
     }
    @Override
    public void eliminar(Rol rol)
    {
      getJdbcTemplate().update(
                "delete from Rol where id = ?, rol.getid_rol()");
    }
    @Override
    public List<Rol> buscarTodos()
    {
     return getSimpleJdbcTemplate().query(
                "select Id_rol from Rol",
                new BeanPropertyRowMapper<Rol>(Rol.class));
            }
    @Override
    public Rol buscar(Integer id)
    {
      try{
          return getSimpleJdbcTemplate().queryForObject("select Id_rol, Descripcion from Rol where Id_rol = ?", new BeanPropertyRowMapper<Rol>(Rol.class),id);
                  
                  }
      catch(EmptyResultDataAccessException e){
          log.error(e.getMessage(),e);
              return null;}
    }

  
    
}
